

#import "AppController.h"



@implementation AppController (TableView)

- (void)initTableViewWithPath:(NSString *)tmp_path
{
	[fileArray removeAllObjects];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *fileList = [fileManager contentsOfDirectoryAtPath:tmp_path error:NULL];
	for (NSString *filename in fileList)
	{
		NSString *realFilePath = [tmp_path stringByAppendingPathComponent:filename];
		NSString* type = [[[NSFileManager defaultManager] fileAttributesAtPath:realFilePath traverseLink:NO] objectForKey:NSFileType];
		if (type!=NSFileTypeDirectory && [Utils fileTypeFilter:filename type:currentType]!=dragDeal_deny) {
//			NSLog(filename);
			[fileArray addObject:[[[AMTFile alloc]initWithName:filename AndTime:@"00:00" AndPath:realFilePath]autorelease]];
		}
		
	}

	[mediaTableView deselectAll:self];
	[mediaTableView reloadData];
}
///////////////////////////
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard
{
	// Drag and drop support
	NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
	[pboard declareTypes:[NSArray arrayWithObject:DragDropTableCellPboardType] owner:self];
	[pboard setData:data forType:DragDropTableCellPboardType];
	return YES;
}

- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
	// Add code here to validate the drop
//	NSLog(@"validate Drop");
	if ([mediaTableView delegate]==nil)
	{
		return NSDragOperationNone;
	}
	
	if ([[[info draggingPasteboard] types] containsObject:NSFilenamesPboardType])
	{
		return NSDragOperationCopy;
	}
	
	if ([[[info draggingPasteboard] types] containsObject:DragDropTableCellPboardType])
	{
		return NSDragOperationMove;
	}
	
	return NSDragOperationNone;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id < NSDraggingInfo >)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
//	NSLog(@"operation Type : %d",operation);
	if(operation == NSDragOperationMove)
	{
		//移动上下TableCell，以后处理
		
		NSData *data = [[info draggingPasteboard] dataForType:DragDropTableCellPboardType];
        NSIndexSet *rowIndexes = (NSIndexSet*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
        if([rowIndexes count]>1) return NO;

//		int index = [rowIndexes firstIndex];
//		NSLog(@"index = %d",index);
		
		return YES;
	}
//	NSLog(@"accept Drop");
	NSPasteboard *pboard;
	pboard = [info draggingPasteboard];
	if ([[pboard types] containsObject:NSFilenamesPboardType])
	{
		//根据文件类型，对文件进行过滤，比如从外部拖入到Music的文件列表中，存在非音频格式的，则不处理，即不添加到列表中去。
		//在这里通过路径和文件名，获取时间和ID3信息，添加到AMTFile类中。
		
		//	//遍历子目录，过滤，拷贝当前Table所支持的格式文件。(弹出对话框，提示，当前的进度，转换中，或者拷贝中，什么的。提供暂停和停止按钮)
		//	//为了能使前台界面可以继续操作，这里需要启动线程，完成转换和拷贝工作，前台可以在对话框中控制进程的运行或者暂停和停止。
		NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
		NSArray *params = [[NSArray alloc]initWithObjects:filenames,[NSNumber numberWithInt:currentType],currentPath,nil];
		[NSThread detachNewThreadSelector:@selector(copyFileThread:) toTarget:self withObject:[params retain]];
		[params release];
		return YES;
	}
	return NO;
}	
///////////////////////////

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
	return [fileArray count];
}

- (id)tableView:(NSTableView *)aTableView 
			objectValueForTableColumn:(NSTableColumn *)column 
			row:(NSInteger)row
{
	AMTFile *amtFile; 
	NSString *identifier; 
	amtFile = [fileArray objectAtIndex:row]; 
	identifier = [column identifier]; 
	[column setEditable:NO];
	return [amtFile valueForKey: identifier]; 
}


//双击事件
///////////
- (IBAction) doubleClicked: (id) sender
{
	if ([mediaTableView numberOfSelectedRows] == 0 || [mediaTableView numberOfSelectedRows] > 1)
    {
		NSBeep();
		return;
    }
	AMTFile *amtFile = [fileArray objectAtIndex:[mediaTableView selectedRow]];
	
	NSLog(@"doubleClicked: %@,%@,%@",[amtFile name],[amtFile time],[amtFile path]);
}

- (void)dealloc 
{
	[fileArray release];
	[super dealloc];
}
@end
